package org.leviatan.definator.client.remote;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Properties;

import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.leviatan.definator.client.DefinatorServiceProxy;
import org.leviatan.definator.client.ejbinterfaces.DefinatorService;
import org.leviatan.definator.client.ejbinterfaces.DefinatorServiceHome;
import org.leviatan.definator.core.model.Field;
import org.leviatan.definator.core.model.Record;
import org.leviatan.definator.core.model.Table;

public class RemoteServiceProxy extends DefinatorServiceProxy {
	
	@SuppressWarnings("unused")
	private String jndi_name = "ejb/DefinatorService";
	
	private Properties jndiProps;
	
	private DefinatorServiceHome serviceHome;
	
	private DefinatorService service;
	
	
	public RemoteServiceProxy() {
		jndiProps = new Properties();
		jndiProps.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
		jndiProps.put("java.naming.provider.url", "jnp://localhost:1099");
		jndiProps.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
		
		try {
			Context ctx = new InitialContext(this.jndiProps);
			
			serviceHome = (DefinatorServiceHome) ctx.lookup(this.jndi_name);
			
			service = serviceHome.create();
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CreateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	@Override
	public HashMap getFieldDefinitions() {
		try {
			return service.getFieldDefinitions();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Field getFieldModel(String aId) {
		try {
			return service.getFieldModel(aId);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public HashMap getRecordDefinitions() {
		try {
			return service.getRecordDefinitions();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Record getRecordModel(String aId) {
		try {
			return service.getRecordModel(aId);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Collection getRecordsFieldIds(String recId) {
		try {
			return service.getRecordsFieldIds(recId);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public HashMap getTableDefinitions() {
		try {
			return service.getTableDefinitions();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Table getTableModel(String aId) {

		try {
			return service.getTableModel(aId);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Collection getTablesRecordIds(String tblId) {
		try {
			return service.getTablesRecordIds(tblId);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	
	///////////////////////////////////////////////////////////////////////////
	
	public String getFieldsRecordId(String fldId) {
		try {
			return service.getFieldsRecordId(fldId);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public String getRecordsTableId(String recId) {
		try {
			return service.getRecordsTableId(recId);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public Field addFieldModel(Field fld) {
		try {
			service.addFieldModel(fld);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public Record addRecordModel(Record rec) {
		try {
			service.addRecordModel(rec);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public Table addTableModel(Table tbl) {
		try {
			service.addTableModel(tbl);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void loadModels() {
		try {
			service.loadModels();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	public boolean removeFieldModel(String fldId) {
		try {
			return service.removeFieldModel(fldId);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean removeRecordModel(String recId) {
		try {
			return service.removeRecordModel(recId);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean removeTableModel(String tblId) {
		try {
			return service.removeTableModel(tblId);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return false;
	}

	public void saveModels() {
		try {
			service.saveModels();
		} catch (RemoteException e) {
			e.printStackTrace();
		}	
	}

	public void loadRegister() {
		try {
			service.loadRegister();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void loadConfiguration(String filePath) {
		try {
			service.loadConfiguration("/home/leviatan/temp/definator-resources/conf/definator.conf");
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	
	
	// updates
	///////////////////////////////////////////////////////////////////////////
	
	@Override
	public boolean updateFieldModel(String fldId, Field fldModel) {
		return false;
	}

	@Override
	public boolean updateRecordModel(String recId, Record recModel) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateTableModel(String tblId, Table tblModel) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateFieldLength(String fldId, int newLength) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateFieldModel(String fldId, String newName, int newLength, String newType, String newRef) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateFieldName(String fldId, String newName) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateFieldRecordRef(String fldId, String newRef) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateFieldType(String fldId, String newType) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateRecordName(String recId, String newName) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateTableName(String tblId, String newName) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String createNewFieldModel() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String createNewFieldModel(String name, int length, String type, String recRef) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String createNewRecordModel() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String createNewRecordModel(String name, String tblRef) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String createNewTableModel() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String createNewTableModel(String name) {
		// TODO Auto-generated method stub
		return null;
	}


	

}
